package controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojos.common.ReplyMessage;
import pojos.student.OrderSubjectInfo;
import pojos.student.StudentInfo;
import service.StudentInfoService;
import utils.InputVerifyUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 学生相关控制器
 */
@Controller
@RequestMapping("/admin")
public class AdminForStudentController {

    @Resource(name = "studentInfoService")
    private StudentInfoService studentInfoService;

    @PostMapping("/frame/addStudent-submit")
    @ResponseBody
    public ReplyMessage addStudent(@RequestParam(value = "loginName",required = false) String loginName,
                                   @RequestParam(value = "loginPswd",required = false) String loginPswd,
                                   @RequestParam(value = "phoneNum", required = false) String phoneNum,
                                   @RequestParam(value = "emailNum", required = false) String emailNum,
                                   @RequestParam(value = "userName",required = false) String userName,
                                   @RequestParam(value = "userSex",required = false) Integer userSex,
                                   @RequestParam(value = "QQ",required = false) String QQ,
                                   @RequestParam(value = "wechat",required = false) String weChat,
                                   @RequestParam(value = "school",required = false) String school,
                                   @RequestParam(value = "grade",required = false) Integer grade,
                                   @RequestParam(value = "comment",required = false) String comment){
        ReplyMessage replyMessage = new ReplyMessage();
        StudentInfo studentInfo = new StudentInfo();

        // 登录名验证
        if(loginName == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录名称不能为空！");
            return replyMessage;
        }
        int loginNameLen = loginName.length();
        if(loginNameLen < 2 || loginNameLen > 20){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录名长度不正确！");
            return replyMessage;
        }

        // 登录密码验证
        if(loginPswd == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录密码不能为空！");
            return replyMessage;
        }
        if(loginPswd.length() != 32){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录密码不正确！");
            return replyMessage;
        }

        // 手机号验证
        if(phoneNum != null && phoneNum.length() > 0){
            if(InputVerifyUtils.isPhoneNotRight(phoneNum)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("手机号不正确！");
                return replyMessage;
            }
            studentInfo.setPhone(phoneNum);
        }

        // 邮箱验证
        if(emailNum != null && emailNum.length() > 0){
            if(InputVerifyUtils.isEmailNotRight(emailNum)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("邮箱号不正确！");
                return replyMessage;
            }
            studentInfo.setEmail(emailNum);
        }

        // 真实姓名验证
        if(userName != null && userName.length() > 0){
            if(InputVerifyUtils.isRealNameNotRight(userName)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("用户真实姓名不正确！");
                return replyMessage;
            }
            studentInfo.setRealName(userName);
        }

        // 性别判断
        if(userSex != null){
            if(userSex == 1){
                studentInfo.setSex((byte) 1);
            }else{
                studentInfo.setSex((byte) 0);
            }
        }

        // QQ号判断
        if(QQ != null && QQ.length() > 0){
            if(InputVerifyUtils.isQQNotRight(QQ)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("QQ号不正确！");
                return replyMessage;
            }
            studentInfo.setQQ(QQ);
        }

        // 微信号判断
        if(weChat != null && weChat.length() > 0){
            if(InputVerifyUtils.isWeChatNotRight(weChat)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("微信号不正确");
                return replyMessage;
            }
            studentInfo.setWeChat(weChat);
        }

        // 学校验证
        if(school != null && school.length() > 0){
            if(InputVerifyUtils.isSchoolNotRight(school)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("学校名称不正确！");
                return replyMessage;
            }
            studentInfo.setSchool(school);
        }

        // 年级判断
        if(grade != null){
            studentInfo.setGrade(grade);
        }

        // 备注验证
        if(comment != null){
            if(comment.length() > 85){
                replyMessage.setStatus(400);
                replyMessage.setInfo("备注不允许超过85个字符！");
                return replyMessage;
            }
            studentInfo.setComment(comment);
        }

        studentInfo.setName(loginName);
        studentInfo.setPassword(loginPswd);
        return studentInfoService.adminAddStudent(studentInfo);
    }

    /**
     * 查询所有的学生姓名
     */
    @PostMapping("/frame/queryAllStudentsName")
    @ResponseBody
    public ReplyMessage queryAllStudentName(){
        return studentInfoService.queryAllStudentsName();
    }

    /**
     * 添加学生预订
     */
    @PostMapping("/frame/addStudentOrder")
    @ResponseBody
    public ReplyMessage addStudentOrder(@RequestParam(value = "student",required = false) Integer student,
                                        @RequestParam(value = "subject",required = false) Integer course,
                                        @RequestParam(value = "money",required = false) String money,
                                        @RequestParam(value = "classNum",required = false) Integer classNum,
                                        @RequestParam(value = "comment",required = false) String comment){
        ReplyMessage replyMessage = new ReplyMessage();
        OrderSubjectInfo subjectInfo = new OrderSubjectInfo();

        if(student == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("请选择学生！");
            return replyMessage;
        }

        if(course == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("请选择课程！");
            return replyMessage;
        }

        if(money != null && money.length() > 0){
            Pattern pattern = Pattern.compile("^\\d{1,5}[.]\\d{2}$");
            Matcher matcher = pattern.matcher(money);
            if(! matcher.matches()){
                replyMessage.setStatus(400);
                replyMessage.setInfo("请输入合法的金额值！");
                return replyMessage;
            }
            BigDecimal bigDecimal = new BigDecimal(money);
            subjectInfo.setOrderMoney(bigDecimal);
        }

        if(classNum != null){
            if(classNum < 0 || classNum > 999){
                replyMessage.setStatus(400);
                replyMessage.setInfo("请输入正确的课时数");
                return replyMessage;
            }
            subjectInfo.setClassNum(classNum);
        }

        if(comment != null){
            if(comment.length() > 250){
                replyMessage.setStatus(400);
                replyMessage.setInfo("请输入正确的备注，长度在250个字符以内！");
                return replyMessage;
            }
            subjectInfo.setCommentA(comment);
        }

        subjectInfo.setStudentId(student);
        subjectInfo.setSubjectId(course);

        return studentInfoService.addStudentOrderClass(subjectInfo);
    }
}
